package Lianxi.ATM;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *  取款功能
 */

public class Get extends JPanel implements ActionListener {
    private Atm frame;

    private JButton returnBut;//创建返回按钮对象
    private JButton confirm;//创建确认按钮对象
    private JTextField takeMoney; //取款文本

    public JTextField getTakeMoney() {
        return takeMoney;
    }

    public void setTakeMoney(JTextField takeMoney) {
        this.takeMoney = takeMoney;
    }

    public Get(Atm frame) {

        this.frame = frame;
        //空布局
        this.setLayout(null);
        //设置透明
        this.setOpaque(false);

        //返回按钮,返回菜单界面
        returnBut = new JButton("返回");
        returnBut.setBounds(350, 600, 100, 50);
        this.add(returnBut);
        returnBut.addActionListener(this);

        //确认按钮,确认取款金额
        confirm = new JButton("确认");
        confirm.setBounds(350, 500, 100, 50);
        this.add(confirm);
        confirm.addActionListener(this);

        takeMoney = new JTextField(); //取款文本框
        takeMoney.setBounds(320, 220, 100, 30);
        this.add(takeMoney);
        takeMoney.addActionListener(this);

        JLabel label=new JLabel("取款：");
        label.setBounds(280,220,80,30);
        this.add(label);


    }


    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == returnBut) {
            frame.switchPanel("菜单");

            //取款
        } else if (e.getSource() == confirm) {
            AtmSql sql = new AtmSql();
            User user = sql.getById((frame.getLoginUser().getId()));
            int money = user.getMoney(); // 获取当前账户金额
            int getMoney = Integer.parseInt(takeMoney.getText()); //获取取出金额
            if (money>= getMoney) {
                money -= getMoney;
                user.setMoney(money);
                sql.update(user,user); //数据库保存
                JOptionPane.showMessageDialog(this, "取出成功, 当前余额：" + user.getMoney() + " 元");

                //数据库导入
//                frame.data();

            } else {
                JOptionPane.showMessageDialog(this, "余额不足！");
            }
        }
    }
}
